#! /usr/bin/python
# -*- coding: utf-8 -*-

import glob, os, shutil, sys

def syncLocal2Remote(local, remote, verbose=True):
    '''
    # Walks through files in defined local directory and looks for these in remote dir.
    # If local file is not found in remote dir, the it is copied there.

    >>> syncLocal2Remote(r'C:/AFT/Logs/', r'G:/sTemp/')
    ERROR: Path G:/sTemp/ seems to be not readable/reachable.
    '''
    if not os.path.exists(remote):
        print 'ERROR: Path ' + remote + ' seems to be not readable/reachable.'
    else:
        # walking trough files in local directory
        for lfn in glob.glob(local + '*'):
            # obtaining local file name lenght
            lfnl = lfn.rfind('\\')
            if lfnl < 0:
                lfnl = lfn.find('/')
            # checking if file does not exist in remote dir
            if not os.path.exists(remote + lfn[lfnl + 1:]):
                # copy local file to remote
                try:
                    if verbose == True: print 'Copying file ' + lfn[lfnl + 1:]
                    shutil.copy (local + lfn[lfnl + 1:], remote + lfn[lfnl + 1:])
                except IOError:
                    print 'ERROR: File ' + remote + lfn[lfnl + 1:] + ' could not be written.'
                except:
                    raise

if __name__ == '__main__':
    ''' '''
    import doctest
    doctest.testmod()

    syncLocal2Remote(r'C:/AFT/sLogs/', r'G:/Temp/')
    syncLocal2Remote(r'C:/AFT/Logs/', r'G:/Temp/')

    
